home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / mac / raytrace / dkb / mcprt102.bnh / DKBTrace-Mac / dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-09  |  9.9 KB  |  367 lines

  1. /*****************************************************************************
  2. *
  3. *                                     dump.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module contains the code to read and write the default file format
  8. *  generated by DKBTrace.  The format is as follows:
  9. *
  10. *
  11. *  (header:)
  12. *    wwww hhhh       - Width, Height (16 bits, LSB first)
  13. *
  14. *  (each scanline:)
  15. *    llll            - Line number (16 bits, LSB first)
  16. *    rr rr rr ...    - Red data for line (8 bits per pixel,
  17. *                       left to right, 0-255 (255=bright, 0=dark))
  18. *    gg gg gg ...    - Green data for line (8 bits per pixel,
  19. *                       left to right, 0-255 (255=bright, 0=dark))
  20. *    bb bb bb ...    - Blue data for line (8 bits per pixel,
  21. *                       left to right, 0-255 (255=bright, 0=dark))
  22. *
  23. * This software is freely distributable. The source and/or object code may be
  24. * copied or uploaded to communications services so long as this notice remains
  25. * at the top of each file.  If any changes are made to the program, you must
  26. * clearly indicate in the documentation and in the programs startup message
  27. * who it was who made the changes. The documentation should also describe what
  28. * those changes were. This software may not be included in whole or in
  29. * part into any commercial package without the express written consent of the
  30. * author.  It may, however, be included in other public domain or freely
  31. * distributed software so long as the proper credit for the software is given.
  32. *
  33. * This software is provided as is without any guarantees or warranty. Although
  34. * the author has attempted to find and correct any bugs in the software, he
  35. * is not responsible for any damage caused by the use of the software.  The
  36. * author is under no obligation to provide service, corrections, or upgrades
  37. * to this package.
  38. *
  39. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  40. * about them.  Also, if you have any comments or questions, you may contact me
  41. * at the following address:
  42. *
  43. *     David Buck
  44. *     22C Sonnet Cres.
  45. *     Nepean Ontario
  46. *     Canada, K2H 8W7
  47. *
  48. *  I can also be reached on the following bulleton boards:
  49. *
  50. *     OMX              (613) 731-3419
  51. *     Mystic           (613) 596-4249  or  (613) 596-4772
  52. *
  53. *  Fidonet:   1:163/109.9
  54. *  Internet:  dbuck@ccs.carleton.ca
  55. *  The "You Can Call Me RAY" BBS    (708) 358-5611
  56. *
  57. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  58. *
  59. *     The "You Can Call Me RAY" BBS (708) 358-5611
  60. *     The Information Exchange BBS  (708) 945-5575
  61. *
  62. *****************************************************************************/
  63.  
  64. #include "frame.h"
  65. #include "dkbproto.h"
  66.  
  67. FILE_HANDLE *Get_Dump_File_Handle()
  68.    {
  69.    FILE_HANDLE *handle;
  70.  
  71.    if ((handle = (FILE_HANDLE *) malloc(sizeof(FILE_HANDLE))) == NULL) {
  72.       fprintf (stderr, "Cannot allocate memory for output file handle\n");
  73.       return(NULL);
  74.       }
  75.  
  76.    handle->Default_File_Name_p = Default_Dump_File_Name;
  77.    handle->Open_File_p = Open_Dump_File;
  78.    handle->Write_Line_p = Write_Dump_Line;
  79.    handle->Read_Line_p = Read_Dump_Line;
  80.    handle->Read_Image_p = Read_Dump_Image;
  81.    handle->Close_File_p = Close_Dump_File;
  82.    return (handle);
  83.    }
  84.  
  85. char *Default_Dump_File_Name()
  86.    {
  87.    return ("data.dis");
  88.    }
  89.  
  90. int Open_Dump_File (handle, name, width, height, buffer_size, mode)
  91.    FILE_HANDLE *handle;
  92.    char *name;
  93.    int *width;
  94.    int *height;
  95.    int buffer_size;
  96.    int mode;
  97.    {
  98.    int data1, data2;
  99.  
  100.    handle->mode = mode;
  101.    handle->filename = name;
  102.  
  103.    switch (mode) {
  104.       case READ_MODE:
  105.          if ((handle->file = fopen (name, "rb")) == NULL) {
  106.             return(0);
  107.             }
  108.  
  109.          if (buffer_size != 0) {
  110.             if ((handle->buffer = malloc (buffer_size)) == NULL)
  111.                return(0);
  112.  
  113.             setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  114.             }
  115.  
  116.          if (((data1 = getc(handle->file)) == EOF)
  117.              || ((data2 = getc(handle->file)) == EOF))
  118.             return(0);
  119.  
  120.          *width  = data2 * 256 + data1;
  121.  
  122.          if (((data1 = getc(handle->file)) == EOF)
  123.              || ((data2 = getc(handle->file)) == EOF))
  124.             return(0);
  125.  
  126.          *height = data2 * 256 + data1;
  127.          handle->width = *width;
  128.          handle->height = *height;
  129.          handle->buffer_size = buffer_size;
  130.          break;
  131.  
  132.       case WRITE_MODE:
  133.          if ((handle->file = fopen (name, "wb")) == NULL)
  134.             return(0);
  135.  
  136.          if (buffer_size != 0) {
  137.             if ((handle->buffer = malloc (buffer_size)) == NULL)
  138.                return(0);
  139.  
  140.             setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  141.             }
  142.  
  143. #ifdef Macintosh
  144.          SetTypeAndCreator(name, Dump_Type, Dump_Creator);
  145. #endif
  146.  
  147.          putc(*width % 256, handle->file);  /* write to either type of file */
  148.          putc(*width / 256, handle->file);
  149.          putc(*height % 256, handle->file);
  150.          putc(*height / 256, handle->file);
  151.  
  152.          handle->width = *width;
  153.          handle->height = *height;
  154.          handle->buffer_size = buffer_size;
  155.  
  156.          break;
  157.  
  158.       case APPEND_MODE:
  159.          if ((handle->file = fopen (name, "ab")) == NULL)
  160.             return(0);
  161.  
  162.          if (buffer_size != 0) {
  163.             if ((handle->buffer = malloc (buffer_size)) == NULL)
  164.                return(0);
  165.  
  166.             setvbuf (handle->file, handle->buffer, _IOFBF, buffer_size);
  167.             }
  168.  
  169.          handle->buffer_size = buffer_size;
  170.          break;
  171.       }
  172.    return(1);
  173.    }
  174.  
  175. void Write_Dump_Line (handle, line_data, line_number)
  176.    FILE_HANDLE *handle;
  177.    COLOUR *line_data;
  178.    int line_number;
  179.    {
  180.    register int x;
  181.  
  182.    putc(line_number % 256, handle->file);
  183.    putc(line_number / 256, handle->file);
  184.  
  185.    for (x = 0 ; x < handle->width ; x++)
  186.       putc((int) floor (line_data[x].Red * 255.0), handle->file);
  187.  
  188.    for (x = 0 ; x < handle->width ; x++)
  189.       putc((int) floor (line_data[x].Green * 255.0), handle->file);
  190.  
  191.    for (x = 0 ; x < handle->width ; x++)
  192.       putc((int) floor (line_data[x].Blue * 255.0), handle->file);
  193.  
  194.    if (handle->buffer_size == 0) {
  195.       fflush(handle->file);                       /* close and reopen file for */
  196.       handle->file = freopen(handle->filename, "ab",
  197.                     handle->file);                /* integrity in case we crash*/
  198.       }
  199.    }
  200.  
  201. int Read_Dump_Line (handle, line_data, line_number)
  202.    FILE_HANDLE *handle;
  203.    COLOUR *line_data;
  204.    int *line_number;
  205.    {
  206.    int data, i, c;
  207.  
  208.    if ((c = getc(handle->file)) == EOF) {
  209.       return (0);
  210.       }
  211.  
  212.    *line_number = c;
  213.  
  214.    if ((c = getc(handle->file)) == EOF)
  215.       return (-1);
  216.  
  217.    *line_number += c*256;
  218.  
  219.    for (i = 0 ; i < handle->width ; i++) {
  220.       if ((data = getc(handle->file)) == EOF)
  221.          return(-1);
  222.  
  223.       line_data[i].Red = (DBL) data / 255.0;
  224.       }
  225.  
  226.    for (i = 0 ; i < handle->width ; i++) {
  227.       if ((data = getc(handle->file)) == EOF)
  228.          return(-1);
  229.  
  230.       line_data[i].Green = (DBL) data / 255.0;
  231.       }
  232.  
  233.    for (i = 0 ; i < handle->width ; i++) {
  234.       if ((data = getc(handle->file)) == EOF)
  235.          return(-1);
  236.  
  237.       line_data[i].Blue = (DBL) data / 255.0;
  238.       }
  239.  
  240.    return (1);
  241.    }
  242.  
  243. int Read_Dump_Int_Line (handle, line_data, line_number)
  244.    FILE_HANDLE *handle;
  245.    IMAGE_LINE *line_data;
  246.    int *line_number;
  247.    {
  248.    int data, i, c;
  249.  
  250.    if ((c = getc(handle->file)) == EOF) {
  251.       return (0);
  252.       }
  253.  
  254.    *line_number = c;
  255.  
  256.    if ((c = getc(handle->file)) == EOF)
  257.       return (-1);
  258.  
  259.    *line_number += c*256;
  260.  
  261.    if (((line_data->red = (unsigned char *) malloc(handle->width))==NULL) ||
  262.        ((line_data->green = (unsigned char *) malloc(handle->width))==NULL) ||
  263.        ((line_data->blue = (unsigned char *) malloc(handle->width))==NULL)) {
  264.       fprintf (stderr, "Cannot allocate memory for picture: %s\n", handle->filename);
  265.       close_all();
  266.       exit(1);
  267.       }
  268.  
  269.    for (i = 0 ; i < handle->width ; i++) {
  270.       line_data->red[i] = 0;
  271.       line_data->green[i] = 0;
  272.       line_data->blue[i] = 0;
  273.       }
  274.  
  275.    for (i = 0 ; i < handle->width ; i++) {
  276.       if ((data = getc(handle->file)) == EOF)
  277.          return(-1);
  278.  
  279.       line_data->red[i] = (unsigned char) data;
  280.       }
  281.  
  282.    for (i = 0 ; i < handle->width ; i++) {
  283.       if ((data = getc(handle->file)) == EOF)
  284.          return(-1);
  285.  
  286.       line_data->green[i] = (unsigned char) data;
  287.       }
  288.  
  289.    for (i = 0 ; i < handle->width ; i++) {
  290.       if ((data = getc(handle->file)) == EOF)
  291.          return(-1);
  292.  
  293.       line_data->blue[i] = (unsigned char) data;
  294.       }
  295.  
  296.    return (1);
  297.    }
  298.  
  299. void Close_Dump_File (handle)
  300.    FILE_HANDLE *handle;
  301.    {
  302.    fclose (handle->file);
  303.    if (handle->buffer_size != 0)
  304.       free (handle->buffer);
  305.    }
  306.  
  307. void Read_Dump_Image(Image, name)
  308.    IMAGE *Image;
  309.    char *name;
  310.    {
  311.    int rc, row, data1, data2;
  312.    struct Image_Line line;
  313.    FILE_HANDLE handle;
  314.  
  315.    if ((handle.file = Locate_File (name, "rb")) == NULL) {
  316.       fprintf (stderr, "Cannot open dump file %s\n", name);
  317.       close_all();
  318.       exit(1);
  319.       }
  320.  
  321.    if (((data1 = getc(handle.file)) == EOF)
  322.        || ((data2 = getc(handle.file)) == EOF)) {
  323.  
  324.       fprintf (stderr, "Cannot open dump file %s\n", name);
  325.       close_all();
  326.       exit(1);
  327.       }
  328.  
  329.    Image->iwidth  = data2 * 256 + data1;
  330.    handle.width = Image->iwidth;
  331.  
  332.    if (((data1 = getc(handle.file)) == EOF)
  333.        || ((data2 = getc(handle.file)) == EOF)) {
  334.  
  335.       fprintf (stderr, "Cannot open dump file %s\n", name);
  336.       close_all();
  337.       exit(1);
  338.       }
  339.  
  340.    Image->iheight = data2 * 256 + data1;
  341.    handle.height = Image->iheight;
  342.  
  343.    Image->width = (DBL)Image->iwidth;
  344.    Image->height = (DBL)Image->iheight;
  345.  
  346.    Image->Colour_Map_Size = 0;
  347.    Image->Colour_Map = NULL;
  348.  
  349.    if ((Image->data.rgb_lines = (struct Image_Line *)
  350.           malloc(Image->iheight * sizeof (struct Image_Line))) == NULL) {
  351.       fprintf (stderr, "Cannot allocate memory for picture: %s\n", name);
  352.       exit(1);
  353.       }
  354.  
  355.    while ((rc = Read_Dump_Int_Line(&handle, &line, &row)) == 1)
  356.       Image->data.rgb_lines[row] = line;
  357.  
  358.    fclose (handle.file);
  359.  
  360.    if (rc == 0)
  361.       return;
  362.    else {
  363.       close_all();
  364.       exit(1);
  365.       }
  366.    }
  367.